package com.sgb.kjwl.view.ui.adapter.enterprise;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemServiecelistBinding;
import com.sgb.kjwl.model.entity.enterprise.ServiceListEntiy;
import com.sgb.kjwl.utils.DialogFunction;
import com.sgb.kjwl.view.ui.activity.enterprise.NewNeedDetailsActivity;
import com.sgb.kjwl.view.ui.activity.self.authentication.CertificationActivity;
import com.sgb.kjwl.view.ui.activity.self.userInfo.VerificationCodeLoginActivity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.SharedPreferenceUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class ServiceListAdpater extends BaseRecycleAdapter<ServiceListEntiy> {
    RequestOptions options;
    RequestOptions teamOptions;
    boolean flag = true; //区分首页及企业服务是否显示关注按钮 false 不显示
    public ServiceListAdpater(Context context, List<ServiceListEntiy> datas) {
        super(context, R.layout.item_serviecelist, datas);
        options = new RequestOptions().placeholder(R.mipmap.gongsiqiye)//图片加载出来前，显示的图片
                .fallback(R.mipmap.gongsiqiye) //url为空的时候,显示的图片
                .error(R.mipmap.gongsiqiye);//图片加载失败后，显示的图片
        teamOptions = new RequestOptions().placeholder(R.mipmap.morentu_tuandui)//图片加载出来前，显示的图片
                .fallback(R.mipmap.morentu_tuandui) //url为空的时候,显示的图片
                .error(R.mipmap.morentu_tuandui);//图片加载失败后，显示的图片
    }

    public ServiceListAdpater(Context context, List<ServiceListEntiy> datas, boolean flag) {
        super(context, R.layout.item_serviecelist, datas);
        options = new RequestOptions().placeholder(R.mipmap.gongsiqiye)//图片加载出来前，显示的图片
                .fallback(R.mipmap.gongsiqiye) //url为空的时候,显示的图片
                .error(R.mipmap.gongsiqiye);//图片加载失败后，显示的图片
        teamOptions = new RequestOptions().placeholder(R.mipmap.morentu_tuandui)//图片加载出来前，显示的图片
                .fallback(R.mipmap.morentu_tuandui) //url为空的时候,显示的图片
                .error(R.mipmap.morentu_tuandui);//图片加载失败后，显示的图片
        this.flag = flag;
    }

    @Override
    public void convert(ItemViewHolder holder, ServiceListEntiy serviceListEntiy) {
        if (holder.getmBinding() instanceof ItemServiecelistBinding) {
            ((ItemServiecelistBinding) holder.getmBinding()).setModel(serviceListEntiy);

            if (flag){
                ((ItemServiecelistBinding) holder.getmBinding()).tvFollow.setVisibility(View.VISIBLE);
            }else {
                ((ItemServiecelistBinding) holder.getmBinding()).tvFollow.setVisibility(View.GONE);
            }

            if (serviceListEntiy.getAttention() != null) {
                setFollwType(((ItemServiecelistBinding) holder.getmBinding()).tvFollow, serviceListEntiy.getAttention());
            } else {
                setFollwType(((ItemServiecelistBinding) holder.getmBinding()).tvFollow, false);
            }

            if (!TextUtils.isEmpty(serviceListEntiy.getAuthTime())) {
                try {
                   long i = Long.parseLong(serviceListEntiy.getAuthTime())/(24*60*60*1000);
                    if(i<365){
                        ((ItemServiecelistBinding) holder.getmBinding()).tvTypeBj.setText("入驻"+i+"天");
                    }else{
                        ((ItemServiecelistBinding) holder.getmBinding()).tvTypeBj.setText("入驻"+(i/365)+"年");
                    }

                }catch (Exception e){
                    e.printStackTrace();
                }

            }

            ((ItemServiecelistBinding) holder.getmBinding()).tvFollow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(onItemClick!=null){
                        onItemClick.onItemClick(serviceListEntiy,-1);
                    }
                }
            });
            try {
                String service = "/";
                if (!TextUtils.isEmpty(serviceListEntiy.getServiceScope()) && serviceListEntiy.getServiceScope().indexOf("[") > -1) {

                    com.alibaba.fastjson.JSONArray jsonArray = com.alibaba.fastjson.JSONArray.parseArray(serviceListEntiy.getServiceScope());
                    if(jsonArray.size()>0){
                        service ="";
                    }
                    for (int i = 0; i < jsonArray.size(); i++) {
                        com.alibaba.fastjson. JSONObject jsonObject = jsonArray.getJSONObject(i);
                        if(jsonObject.get("name")!=null){
                            service = service+jsonObject.getString("name")+"、";
                        }
                    }
                    if(!TextUtils.isEmpty(service)&&service.indexOf("、")>-1){
                        service = service.substring(0,service.length()-1);
                    }

                }

                ((ItemServiecelistBinding) holder.getmBinding()).tvServiceFw.setText(service);

            }catch (Exception e){
                e.printStackTrace();
            }
            Glide.with(context).load(serviceListEntiy.getImg()).apply(serviceListEntiy.getPromulgatorType()==1?options:teamOptions).into( ((ItemServiecelistBinding) holder.getmBinding()).ivPublishType);

        }


    }


    private void setFollwType(TextView textView, boolean flag) {
        textView.setText(flag ? "已关注" : "关注");
        textView.setBackgroundResource(flag ? R.drawable.shape_colorc0c4cc_corneras10 : R.drawable.shape_red_btn_line_corneras10);
        textView.setTextColor(flag ? Color.parseColor("#969696") : Color.parseColor("#EF4033"));
        textView.setTextColor(flag ? Color.parseColor("#969696") : Color.parseColor("#ef4033"));


    }
}
